<?php





namespace app\api\controller;



use app\admin\model\BydAgreement;
use app\admin\model\BydDesfollow;
use app\admin\model\BydDesignerArea;
use app\admin\model\BydDesstar;
use app\admin\model\BydDestri;

use app\admin\model\Bydletter;
use app\admin\model\BydShopOrder;
use app\admin\model\BydUserSale;
use app\admin\model\BydZan;
use app\admin\model\DesPf;
use app\admin\model\SoleAgency;
use app\admin\model\SoleBuilding;
use app\common\controller\News;
use base\Wx;

use app\admin\model\BydDesigner;

use app\admin\model\BydDesignerMake;

use app\admin\model\BydDesignerPro;

use app\admin\model\BydDesMode;

use app\admin\model\BydDispro;

use app\admin\model\BydDistri;

use app\admin\model\BydPl;

use app\common\controller\Api;



class Bydidentity extends Api

{

    /**

     * 无需登录的方法,同时也就不需要鉴权了

     * @var array

     */

    protected $noNeedLogin = ['*'];

    /**

     * 身份专区

     * @param integer $id 用户id
      type 0为c端1为设计师端
     */

    public function shenfen(){

        $data = $this->request->request('');
        $user=\app\admin\model\BydUser::find($data['id']);//

        if($data['type']==0){
            $user=BydUserSale::where('user_id',$data['id'])->order('id desc')->find();
            $id=['1','2','3'];
            $iden=\app\admin\model\BydIden::where('id','in',$id)->select();//所有身份
            foreach ($iden as &$v){
//                $where['user_id']=['eq',$data['id']];
//                $where['lev']=['eq',$data['id']];
//                $where['user_id']=['eq',$data['id']];

                $find=BydUserSale::where('user_id',$data['id'])->where('level',$v['id'])->where('state',0)->order('id desc')->find();
                if(empty($find)){
                    $v['pd']=0;
                }else{
                    $v['pd']=1;
                }
            }

        }else{
            $user=BydDesigner::where('user_id',$data['id'])->order('id desc')->find();
            $id=['3','4','5'];
            $iden=\app\admin\model\BydIdentity::where('id','in',$id)->select();//所有身份
            foreach ($iden as &$v){
//                $where['user_id']=['eq',$data['id']];
//                $where['lev']=['eq',$data['id']];
//                $where['user_id']=['eq',$data['id']];

                $find=BydDesigner::where('user_id',$data['id'])->where('level',$v['id'])->where('state',0)->find();
                if(empty($find)){
                    $v['pd']=0;
                }else{
                    $v['pd']=1;
                }
            }
        }

        if($iden){

            $this->success(__('请求成功'),['status' =>$iden,'user'=>$user]);

        }else{

            $this->error('暂无数据');

        }

    }



    /**

     * 设计师标签

     */

    public function desmode(){

        $arr=BydDesMode::select();

        if ($arr){

            $this->success(__('请求成功'),['status' => $arr]);

        }else{

            $this->error('暂无数据');

        }

    }

    /***
     * 设计师修改资料
     *@param integer $id 设计师id
     */

    public function editdes(){

        $request=request();

        $data=$request->param();

        $save=new BydDesigner();

        $where['id']=$data['id'];

        $edit=$save->isUpdate(true,$where)->save($data);

        if($edit){

            $this->success(__('修改成功'));

        }else{

            $this->error('修改失败');

        }

    }

    function validation_filter_id_card($id_card){

        if(strlen($id_card)==18){

            return $this->idcard_checksum18($id_card);

        }elseif((strlen($id_card)==15)){

            $id_card=$this->idcard_15to18($id_card);

            return $this->idcard_checksum18($id_card);

        }else{

            return false;

        }

    }

// 计算身份证校验码，根据国家标准GB 11643-1999

    function idcard_verify_number($idcard_base){

        if(strlen($idcard_base)!=17){

            return false;

        }

        //加权因子

        $factor=array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);

        //校验码对应值

        $verify_number_list=array('1','0','X','9','8','7','6','5','4','3','2');

        $checksum=0;

        for($i=0;$i<strlen($idcard_base);$i++){

            $checksum += substr($idcard_base,$i,1) * $factor[$i];

        }

        $mod=$checksum % 11;

        $verify_number=$verify_number_list[$mod];

        return $verify_number;

    }

// 将15位身份证升级到18位

    function idcard_15to18($idcard){

        if(strlen($idcard)!=15){

            return false;

        }else{

            // 如果身份证顺序码是996 997 998 999，这些是为百岁以上老人的特殊编码

            if(array_search(substr($idcard,12,3),array('996','997','998','999')) !== false){

                $idcard=substr($idcard,0,6).'18'.substr($idcard,6,9);

            }else{

                $idcard=substr($idcard,0,6).'19'.substr($idcard,6,9);

            }

        }

        $idcard=$idcard.$this->idcard_verify_number($idcard);

        return $idcard;

    }

// 18位身份证校验码有效性检查

    function idcard_checksum18($idcard){

        if(strlen($idcard)!=18){

            return false;

        }

        $idcard_base=substr($idcard,0,17);

        if($this->idcard_verify_number($idcard_base)!=strtoupper(substr($idcard,17,1))){
            return false;
        }else{
            return true;
        }

    }
    /**
     *
     */


    /**

     * 申请成为设计师



     *  @param integer $user_id` '用户id',

     *  @param integer $idcart` '身份证号码',

     *  @param integer $address` '所在地区',

     *  @param integer $email` '电子邮箱',

     *  @param integer $mobile` '手机号',

     *  @param integer $level`  '身份',

     *  @param integer $levelname` '身份名称',


     */

  public function adddes(){
     // \think\Cache::set('des',1,3600);die;
      $request=request();

      $data=$request->param();
       $user=\app\admin\model\BydUser::find($data['user_id']);
       $data['avatar']=$user['avatar'];
if(!is_numeric($data['level']))  $this->error('身份信息错误');
//      $idcar=$this->validation_filter_id_card($data['idcart']);

//      if($idcar==false){

//          $this->error('请输入正确的身份证号码');

//      }

      $add=\app\api\model\BydIdentity::adddes($data);

      if($add==false){

          $this->error('请输入正确的电话号码');

      }elseif($add==null){

          $this->error('请求失败');

      } else{
          \think\Cache::set('des',1,3600);
          $this->success(__('请求成功'));

      }

  }

    /**

     * 设计师---系统消息

     *  @param integer $id` '设计师id',

     */

    public function addnews(){

        $data = $this->request->request('');

        $arr=BydDesignerMake::where('des_id',$data['id'])->select();//->where('state',0)

        if($arr){

            $this->success(__('请求成功'),['status'=>$arr]);

        }else{

            $this->error('暂无数据');

        }

    }

    /**

     * 设计师订单---拒绝/接单/确认完成
     *  @param integer $id` '设计师id',
     *  @param integer $state` '0为拒绝1为接单2为确认完成',

     * id  订单id

     * state 0为拒绝1为接单2为确认完成

    */

    public function desorder(){



        $data = $this->request->request('');
    if(empty($data['id'] || empty($data['state'])))$this->error('参数不正确');
        $order=new BydDesignerMake();

        $find=$order->where('id',$data['id'])->find();
        $save=$order->isUpdate(true,['id'=>$data['id']])->save(['state'=>$data['state']]);

        if($data['state']==0){
            $saves=$order->isUpdate(true,['id'=>$data['id']])->save(['des_id'=>0,'bhnew'=>$data['bhnew'],'jjtime'=>time()]);
        }elseif ($data['state']==1){
            $saves=$order->isUpdate(true,['id'=>$data['id']])->save(['jdtime'=>time()]);
            $add=new Bydletter();
            $add->data([
                'user_id'=>$find['user_id'],
                'orderid'=>$find['orderid'],
                'content'=>'设计师已接单,请前往我的预约记录查看',
                'zt'=>'已接单',
                'state'=>1
            ]);
            $add->save();
            $user=\app\admin\model\BydUser::find($find['user_id']);
            $arr=[
                'openid'=>$user['openid'],
                'thing1'=>"预约订单最新状态",
                'thing4'=>"您的预约订单设计师已接单请前往小程序->我的预约记录中查看",
                'time'=>date('Y年m月d日 H:i',time()),
                'thing5'=>"请前往设计师端完成资料",
            ];
            $new=new News();
            $news=$new->liuyan($arr);
        }

        if($save){
            \think\Cache::set('desyy',1,3600);
            $this->success(__('请求成功'));

        }else{

            $this->error('请求失败');

        }

    }
    /**
     * 个人资料
     *    @param integer $user_id` '用户id',
     *
     */
    public function myuser(){
        $request=request();
        $data=$request->param();
        if (empty($data['user_id']) )$this->error('请传参数');
        $find=\app\admin\model\BydDesigner::where('user_id',$data['user_id'])->find();
        if(empty($find))$this->error('用户不存在,请重新登录');
        $user=\app\admin\model\BydUser::find($data['user_id']);
        $find['bbs']=\app\admin\model\BydIdentity::find($user['des']);
        if($find){
            $this->success(__('请求成功'),['myuser'=>$find]);
        }else{
            $this->error('请求失败');
        }
    }
    /**

     * 订单
     *    @param integer $id` '用户id',
     * id

     */

    public function desorders(){

        $data = $this->request->request('');
        //$where['createtime']=['between time',[$data['start'],$data['end']]];
        $find=BydDesigner::where('user_id',$data['id'])->find();
     if(empty($find))   $this->error('设计师不存在');
        $where['des_id']=['eq',$find['id']];
        $arr=BydDesignerMake::where('des_id',$find['id'])->where('state','>',0)->select();
        $dipro=[];
        foreach ($arr as &$v){
            $finddes=BydShopOrder::where('yyid',$v['orderid'])->find();
            if(!empty($finddes)){
                $dipro=BydDispro::where('order_id',$v['orderid'])->find();

                $v['dipro']=$dipro;
            }else{
                $dipro=[];
                $v['dipro']=$dipro;
            }
            $v['statess']=BydShopOrder::where('yyid',$v['orderid'])->field('state')->find();
        }

        if($arr){
            $this->success(__('请求成功'),['status'=>$arr]);
        }else{
            $this->error('暂无数据');
        }

    }

    /**

     * 设计师订单--上传二维码
     *    @param integer $id` '订单id',
     *    @param integer $ewm` '二维码',
     * id  订单id

     *  ewm 二维码

     */

    public function upewm(){

        $data = $this->request->request('');

        $order=new BydDesignerMake();

        $save=$order->isUpdate(true,['id'=>$data['id']])->save(['ewm'=>$data['ewm']]);

        if($save){

            $this->success(__('上传成功'));

        }else{

            $this->error('上传失败');

        }

    }



        /**

         * 设计师个人资料

         *   @param integer $id  设计师id,

         */

        public function desindex(){

            $data = $this->request->request('');

            $find=BydDesigner::find($data['id']);

         $user=\app\admin\model\BydUser::find($find['user_id']);
         $find['zw']=\app\admin\model\BydIdentity::find($user['des']);
            if($find){
                $this->success(__('请求成功'),['status' => $find]);
            }else {
                $this->error('暂无数据');
            }
        }

    /**

     * 设计师上传方案

     * @param integer $des_id  设计师id,

     */

    public function addprogramme(){

        $request=request();
        $data=$request->param();

   $des=BydDesigner::find($data['des_id']);
   $user=\app\admin\model\BydUser::find($des['user_id']);
$iden=\app\admin\model\BydIdentity::find($user['des']);
$data['level']=$iden['type'];
        $add=\app\api\model\BydIdentity::addpro($data);

        if($add==null){

            $this->error('上传失败');

        }else {

            $this->success(__('上传成功'));

        }

    }



    /**

     * 设计师首页
     * @param integer $user_id` '用户id',


     * user_id 用户id

     */

    public function des(){

        $data = $this->request->request('');
//        $arr=BydDistri::whereOr(
//            ['pid'=>1,
//                'family'=>1,
//            ])->count();
//        print_r($arr);die;

       $find=BydDesigner::where('user_id',$data['user_id'])->where('state',1)->find();

       if($find){
            $user=\app\admin\model\BydUser::find($data['user_id']);
           $find['zp']=BydDesignerPro::where('des_id',$find['id'])->where('state',1)->select();
           $find['des']=\app\admin\model\BydIdentity::find($user['des']);
           if($find){
               $this->success(__('请求成功'),['status' => $find]);
           }else {
               $this->error('暂无数据');
           }

       }else{

           $this->error('请前往平台注册');

       }



    }

  /**

   * 设计师列表
   * @param integer $order` '综合排序：0为智能排序1星级排序2为设计师单量排序3为好评排序4为点赞两排序5为关注量排序',
     order  综合排序：0为智能排序1星级排序2为设计师单量排序3为好评排序4为点赞两排序5为关注量排序
   */

public function dessel(){
    $data = array();
    $data = $this->request->request('');
    if(!isset($data['order'])) $data['order'] = 0;
    if($data['order']){
        switch ($data['order']) {
            case 0:
                $arr=BydDesigner::order('createtime desc')->select();
                break;
            case 1:
                $arr=BydDesigner::order('star desc')->select();
                break;
            case 2:
                $arr=BydDesigner::order('id desc')->select();
                break;
            case 3:
                $arr=BydDesigner::order('plcount desc')->select();
                break;
            case 4:
                $arr=BydDesigner::order('zan desc')->select();
                break;
            case 5:
                $arr=BydDesigner::order('follow desc')->select();
                break;
        }
    }else{
        $arr=BydDesigner::order('id desc')->select();
    }
    if($arr){
        $url = '';
        $url = ((int)$_SERVER['SERVER_PORT'] == 80 ? 'http://' : 'https://') . $_SERVER['HTTP_HOST'];

        foreach ($arr as $key => &$value) {
            //$value['avatar'] ="http://" . $_SERVER['SERVER_NAME'] . $value['avatar'];
            if(trim($value['avatar']) != '')            $value['avatar'] =$url. $value['avatar'];
            $value['content'] = '';
        }


        $this->success(__('请求成功'),['status' => $arr]);

    }else {

        $this->error('暂无数据');

    }

}

    /**

     * 设计师详情
     * @param integer $id` '设计师id',

     */

    public function desxq(){
        $data = $this->request->request('');
        $find=BydDesigner::where('id',$data['id'])->find();
        $add=BydDesigner::where('id',$data['id'])->setInc('look',1);
        if(empty($find)){
            $this->error('暂无数据');
        }
        if($find['star']==0){
            //设计师的60%
            $allscore=$find['manyi']+$find['cy']+$find['yj']+$find['zy'];//总分数
            $bl=$allscore/400;//比例
            $destar=$bl*3;
            $num1=round($destar);
            //用户的40%
            $pfcount=DesPf::where('des_id',$data['id'])->count();
            if(empty($pfcount)){
                $find['star']=$num1+2;
            }else{
                $countall=$pfcount*100;
                $pfall=DesPf::where('des_id',$data['id'])->sum('all');//用户评论的总分
                $pj=$pfall/$countall;//平均出来的
                $bll=$pj*2;
                $num2=round($bll);
                $find['star']=$num1+$num2;
            }

        }
        $find['zans']=BydZan::where('video_id',$data['id'])->where('user_id',$data['user_id'])->where('state',1)->find();
        $find['userstar']=BydDesstar::where('des_id',$data['id'])->where('user_id',$data['user_id'])->find();
        $find['zp']=BydDesignerPro::where('des_id',$find['id'])->select();
        $follows=BydDesfollow::where('text_id',$find['id'])->where('user_id',$data['user_id'])->find();
        if($follows){
            $find['follows']=0;
        }else{
            $find['follows']=1;
        }
        $pl=db('BydPl')->where('dis_id',$find['id'])->order('id desc')->select();
        if($pl){

            foreach ($pl as $k => &$v) {

                $wheree['id'] = $v['user_id'];

                $usfind = db('BydUser')->where($wheree)->field('username,avatar')->find();//找到此条评论的用户新西

                $v['name'] = $usfind['username'];

                $v['head'] = $usfind['avatar'];

                $wh['pl_id'] = $v['id'];

                // $wh['pl_user_id'] = $v['user_id'];

                $v['list']=db('BydPlhf')->where($wh)->order('id desc')->select();//找到此条评论的回复

                if($v['list']){

                    foreach ($v['list'] as &$q) {

                        $q['bhhfuser'] = db('BydUser')->where('id', $q['pl_user_id'])->field('username,avatar')->find();//找到被回复人的信息

                        $qqq= db('BydUser')->where('id', $q['user_id'])->field('username,avatar')->find();//找到回复人的信息

                        $q['user']=$qqq;

                    }

//$v['hf']=$rrs;

                }

            }

        }

        $find['pl']=$pl;

        if($find){

            if($find){

                $this->success(__('请求成功'),['status' => $find]);

            }else {

                $this->error('暂无数据');

            }

        }else{

            $this->error('请前往平台注册');

        }

    }

/**

 * 我的团队

 * @param integer $user_id  用户id

 *    @param integer $state  级别 1为1级2为2级

 */

public function myteam(){

    $data= $this->request->request('');

    //$arr=\app\api\model\BydIdentity::myteam($data);



        if($data['state']==1){

            $arr=db('BydDistri')->where('pid',$data['user_id'])->select();

        }else{
            $where['family']=array('eq',$data['user_id']);
            $where['pid']=array('neq',$data['user_id']);
            $arr=BydDistri::where($where)->select();

        }

        foreach ($arr as &$v){

            $v['users']=\app\admin\model\BydUser::find($v['user_id']);//db('BydUser')->where('id',$v['user_id'])->find();

            $v['moneys']=BydDispro::where('pid',$data['user_id'])->where('user_id',$v['user_id'])->where('type',1)->sum('price');

            $v['son']=BydDistri::whereOr(

                ['pid'=>$v['user_id'],

                    'family'=>$v['user_id'],

                ])->count();

        }

        $count=BydDistri::whereOr(

            ['pid'=>$data['user_id'],

                'family'=>$data['user_id'],

            ])->count();//总人数whereOr(



    $sums=BydDispro::where(

            ['pid'=>$data['user_id'],



            ])->sum('price');//总返利

    $sum=round($sums,2);

//        if($arr){

//            return $arr;

//        }else{

//            return null;

//        }



    if($arr){

        //return json(['code'=>1,'msg'=>'请求成功','data'=>$arr,'count'=>$count,'sum'=>$sum]);

        $this->success(__('请求成功'), ['status' => $arr,'count'=>$count,'sum'=>$sum]);



    }else {

        $this->error('暂无数据');

    }

}



    /**

     * 设计师我的团队

     * @param integer $user_id  用户id

     *    @param integer $state  级别 1为1级2为2级

     */

    public function desmyteam(){

        $data= $this->request->request('');

        //$arr=\app\api\model\BydIdentity::myteam($data);



        if($data['state']==1){

            $arr=db('BydDestri')->where('pid',$data['user_id'])->select();

        }else{

            $where['family']=array('eq',$data['user_id']);
            $where['pid']=array('neq',$data['user_id']);
            $arr=BydDestri::where($where)->select();

        }

        foreach ($arr as &$v){

            $v['users']=\app\admin\model\BydUser::find($v['user_id']);//db('BydUser')->where('id',$v['user_id'])->find();

            $v['moneys']=BydDispro::where('pid',$data['user_id'])->where('user_id',$v['user_id'])->sum('price');

            $v['son']=BydDestri::whereOr(

                ['pid'=>$v['user_id'],

                    'family'=>$v['user_id'],

                ])->count();

        }

        $count=BydDestri::whereOr(

            ['pid'=>$data['user_id'],

                'family'=>$data['user_id'],

            ])->count();//总人数whereOr(



        $sum=BydDispro::where(

            ['pid'=>$data['user_id'],



            ])->sum('price');//总返利

//        if($arr){

//            return $arr;

//        }else{

//            return null;

//        }



        if($arr){

            //return json(['code'=>1,'msg'=>'请求成功','data'=>$arr,'count'=>$count,'sum'=>$sum]);

            $this->success(__('请求成功'), ['status' => $arr,'count'=>$count,'sum'=>$sum]);



        }else {

            $this->error('暂无数据');

        }

    }

    /**

     * 我的佣金

     * @param integer $user_id  用户id

     */

    public function mysub(){

        $data= $this->request->request('');

//       $arr=\app\api\model\BydIdentity::mysub($data);

        $where['createtime']=['between time',[$data['start'],$data['end']]];
        $where['pid']=['eq',$data['user_id']];
//        $where['level']=['<',4];
        $arr=db('BydDispro')->where($where)->order('id desc')->select();

        $allprice=BydDispro::where('pid',$data['user_id'])->sum('price');

        $canprice=BydDispro::where('pid',$data['user_id'])->where('tixian',0)->sum('price');

        if($arr){

            $this->success(__('请求成功'),['status' => $arr,'all'=>$allprice,'can'=>$canprice]);

//            $this->error('暂无数据');

        }else {

            $this->error('暂无数据');

//            $this->success(__('请求成功'),['status' => $arr]);

        }

    }



    /**

     * 预约订单支付定金/尾金
     * @param integer $type` '0为定金1为尾金',
     * @param integer $id` '订单id',
     * @param integer $user_id` '用户id',

     * type 0为定金1为尾金

     * id  订单id

     * user_id用户

     */

    public function paymoney(){

        $data= $this->request->request('');

        $find=BydDesignerMake::find($data['id']);

        if($data['type']==0){

            $data['price']=$find['price'];

            $osn ="D". date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);//订单号

            $save=BydDesignerMake::where('id',$data['id'])->update(['dorderid'=>$osn]);

        }else{

            $data['price']=$find['wprice'];

            $osn ="W". date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);//订单号

            $save=BydDesignerMake::where('id',$data['id'])->update(['worderid'=>$osn]);

        }

        $user=\app\admin\model\BydUser::find($data['user_id']);

        $config = [

            'app_id'    => 'wx00e9e5b843259a9d',

            'mch_id'    => '1584376091',

            'key'       => 'TjRUCktaUcR6RQqCksBtwKRY8KwSTC4M',

            'secret'    => 'cce6015196dbba888133f9a3613b18b7',

            'notify'    => 'http://bao.scwushen.com/index.php/api/notify/cg',

        ];

        $obj = new Wx( $config );

        $money = 1;

        $data = [

            'body' => '身份购买',

            'out_trade_no' => $osn, //客户订单号,

            'total_fee'=> $data['price'] * 100,

            'trade_type'    => 'JSAPI',

            'openid'    => $user['openid'],

        ];

        $ret = $obj->unifiedOrder( $data );

        return json($ret);

    }
    /**
     * 查询是否支付
     * @param integer $user_id` '用户id',
     * user_id
     */
public function ispay(){
    $data= $this->request->request('');
    $find=BydDispro::where('user_id',$data['user_id'])->where('level',$data['id'])->where('state',1)->find();
    if($find){
        $this->success(__('您已购买 请填写资料'));
    }else{
        $this->error('暂未购买');
    }
}
    /**

     * 立即购买
     * @param integer $id` '身份得id',
     * @param integer $user_id` '用户id',

     * id  身份得id

     * user_id  用户id

     */

    public function buylevel(){

        $data= $this->request->request('');
//if($data['level']==0){
//
//}
        if($data['level']==0){

            $osn = "byd".date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);//订单号
            $find=BydDispro::where('user_id',$data['user_id'])->where('level',$data['id'])->where('state',1)->find();

            if($find){

                $this->success(__('您已购买 请填写资料'));

            }
            $find=\app\admin\model\BydIden::where('id',$data['id'])->find();//查询身份分佣金额
            if($find['money']==0){
                $this->success(__('请填写资料'));
            }
        }else{

            $find=BydDispro::where('user_id',$data['user_id'])->where('level',$data['id'])->where('state',1)->find();
            if($find){
                $finddes=BydDesigner::where('user_id',$data['user_id'])->where('state',1)->find();
                if(empty($finddes)){
                    $this->success(__('您已购买 请填写资料'));
                }else{

                    $edit=BydDesigner::where('user_id',$data['user_id'])->where('state',1)->update(['state'=>0,'level'=>$data['id']]);
                    \think\Cache::set('des',1,3600);
                    $this->success(__('升级成功,请等待平台审核'));
                }

            }
            $osn = "des".date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);//订单号
            $find=\app\admin\model\BydIdentity::where('id',$data['id'])->find();//查询身份分佣金额
            if($find['money']==0){
                $this->success(__('请填写资料'));
            }
        }



        $user=\app\api\model\BydUser::where('id',$data['user_id'])->find();

        $add = new BydDispro();

        $add->data([

            'cjprice'=>$find['money'],

            'level'=>$find['id'],

            'pid'  => 0,

            'pay'  => 0,

            'state' =>  0,


            'user_id' =>  $data['user_id'],

            'type' => 1,

            'order_id'=>$osn,

            'money'=>0,

            'price'=>0

        ]);

        $add->save();

      if($add){

          $config = [

              'app_id'    => 'wx00e9e5b843259a9d',

              'mch_id'    => '1584376091',

              'key'       => 'TjRUCktaUcR6RQqCksBtwKRY8KwSTC4M',

              'secret'    => 'cce6015196dbba888133f9a3613b18b7',

              'notify'    => 'http://bao.scwushen.com/index.php/api/notify/cg',

          ];

          $obj = new Wx( $config );

          $money = 1;

          $data = [

              'body' => '身份购买',

              'out_trade_no' => $osn, //客户订单号,

              'total_fee'=> $find['money'] * 100,

              'trade_type'    => 'JSAPI',

              'openid'    => $user['openid'],

          ];

          $ret = $obj->unifiedOrder( $data );

          return json($ret);

      }



    }

    function object_array($array) {
        if(is_object($array)) {
            $array = (array)$array;
        }
        if(is_array($array)) {
            foreach($array as $key=>$value) {
                $array[$key] =$this->object_array($value);
            }
        }
        return $array;
    }
    /**

     * 预约设计师
     * @param integer $user_id` '用户id',

     */

   public function yydes(){

       $request=request();
       $data=$request->param();
       $time=time();
       $ems=new Ems();
//       $yz=$ems->sendsyz($data['phone'],$data['yzm']);
//       $array = json_decode($yz,true);
//       if($array['code']==0)$this->error('验证码错误');
       $data['orderid']="yy". date('YmdHis',$time) . round(1000,9999);
       $source = $data['people'];//按逗号分离字符串
       $hello = explode(',',$source);
       foreach ($hello as $v){
           $qq = explode('_',$v);
           if($qq[1]==0){
               $sex='先生';
           }elseif($qq[1]==1){
               $sex='女士';
           }elseif($qq[1]==2){
               $sex='男孩';
           }elseif($qq[1]==3){
               $sex='女孩';
           }
           $arr="$sex-$qq[0]岁";
           $qwe[]=$arr;
       }
       $comma_separated = implode(",", $qwe);
       $data['peoples']=$comma_separated;
       //判断是否有正在进行中的订单
       $wheres['user_id']=['eq',$data['user_id']];
       $wheres['state']=['eq',1];
//       $find=BydDesignerMake::where($wheres)->find();
//       if(!empty($find))$this->error('您有正在进行中的订单,请完成后再预约');
       $create=BydDesignerMake::create($data);
       if($create){
           \think\Cache::set('yydes',1,3600);
           $this->success(__('请求成功'));

       }else{

           $this->error('暂无数据');

       }

   }

   public function qq(){

      $arr=[

          'idcar'=>2,

          'user_id'=>62

      ];

      $tt=\app\api\model\BydIdentity::xsub($arr);

      print_r($tt);die;

   }

    /**

     * 合同

     */

    public function contein(){

        $url="http://bao.scwushen.com/uploads/contract.docx";

        $this->success(__('请求成功'),['status'=>$url]);

    }

    /**

     * 设计师申请状态
     * @param integer $user_id` '用户id',
     * user_id 用户id

     */

    public function despro(){

        $data= $this->request->request('');
      $des=BydDesigner::where('user_id',$data['user_id'])->find();
      if($des){
          $this->success(__('请求成功'),['status'=>$des]);
      }else{
          $this->error('暂无数据');

      }

    }

    /**

     * 设计师订单搜索
     * @param integer $name` '搜索的内容',
     *  name  搜索的内容
     */

    public function sousuo(){

        $data = $this->request->request('');
        $map['name'] = ['like', '%' . $data['name'] . '%'];
        $arr = BydDesignerMake::where($map)->select();
        if ($arr) {
            $this->success(__('请求成功'), ['status' => $arr]);
        } else {
            $this->error('暂无数据');
        }

    }

    /**

     * 设计师价格

     */

    public function desproMoenys(){

        $find=\app\admin\model\BydIdentity::where('id','>',2)->select();

        if($find){

            $this->success(__('请求成功'),['status'=>$find,'edit'=>false]);//true为一二级设计师，false为二三级设计师

        }
    }
/**
 * 给设计师打星
 * @param integer $user_id` '用户id',
 * @param integer $des_id` '设计师id',
 * @param integer $star` '星星数量',
 * user_id
 * des_id
 * star
 */
public function star(){
    $data = $this->request->request('');
    $where =  [
        'user_id'  => $data['user_id'],
        'des_id'  => $data['des_id'],
//        'num'  => $data['star'],
    ];
    $find=BydDesstar::where($where)->find();
    if(empty($find)){
        $add=new BydDesstar();
        $add->data([
            'des_id'  => $data['des_id'],
            'user_id'  => $data['user_id'],
            'num'  => $data['star'],
        ]);
        $res = $add->save();
        if ($res) {
            $this->success(__('打星成功'));
        } else {
            $this->error('打星失败');
        }

    }else{
        $this->error('请勿重复打星');
    }
}

/**
 *  设计师方案删除
 * @param integer $id` '方案的id',
 * id
 */
public function delefa(){
    $data = $this->request->request('');
    $del=BydDesignerPro::where('id',$data['id'])->delete();
    if($del){
        $this->success(__('删除成功'));
    }else{
        $this->error('删除失败');
    }
}
/**
 * 设计师根据标签筛选
 *  @param integer $label` '筛选条件',
 */
public function deslabel(){
    $data = $this->request->request('');
    if (empty($data['label'])) $this->error('筛选条件不能为空');
    $where['label']=['like','%'.$data['label'].'%'];
    $where['state']=['eq',1];
    $arr=BydDesigner::where($where)->select();
    if (empty($arr)) $this->error('暂无数据');
    $this->success(__('请求成功'),['status'=>$arr]);
}
/**
 * 查看所有协议
 * @param integer $state` '协议的类型',
 */
public function agreement(){
    $data = $this->request->request('');
    $arr=BydAgreement::where('switch',1)->where('state',$data['state'])->select();

    if(empty($arr)) $this->error('暂无数据');
    foreach ($arr as $v){
        $arrs=$v['content'];
        $arrs=str_replace('{$star}','当前时间',$arrs);
        $arrs=str_replace('{$end}','明年截至时间',$arrs);
        $arrs=str_replace('{$name}','乙方姓名',$arrs);
        $arrs=str_replace('{$oname}','乙方姓名',$arrs);
        $arrs=str_replace('{$sheng}','xxx',$arrs);
        $arrs=str_replace('{$shi}','xxx',$arrs);
        $arrs=str_replace('{$qu}','xxx',$arrs);
        $arrs=str_replace('{$lp}','xxx',$arrs);
        $v['content']=$arrs;
    }

    $this->success(__('请求成功'),['status'=>$arr]);
}
    public function agreements(){
        $data = $this->request->request('');
        $where['state']=['neq',13];
        $where['switch']=['eq',1];
        $arr=BydAgreement::where($where)->select();
        foreach ($arr as $v){
            $arrs=$v['content'];
            $arrs=str_replace('{$star}','当前时间',$arrs);
            $arrs=str_replace('{$end}','明年截至时间',$arrs);
            $arrs=str_replace('{$name}','乙方姓名',$arrs);
            $arrs=str_replace('{$oname}','乙方姓名',$arrs);
            $arrs=str_replace('{$sheng}','xxx',$arrs);
            $arrs=str_replace('{$shi}','xxx',$arrs);
            $arrs=str_replace('{$qu}','xxx',$arrs);
            $arrs=str_replace('{$lp}','xxx',$arrs);
            $v['content']=$arrs;
        }
        if(empty($arr)) $this->error('暂无数据');
        $this->success(__('请求成功'),['status'=>$arr]);
    }
    /**
     * 申请成为销售员
     *  *  @param integer $user_id` '用户id',


     *  @param integer $idcart` '身份证号码',

     *  @param integer $address` '所在地区',

     *  @param integer $email` '电子邮箱',

     *  @param integer $mobile` '手机号',

     *  @param integer $level`  '身份',

     *  @param integer $levelname` '身份名称',

     */
    public function sqb(){
        $data = $this->request->request('');
        if(!empty($data['mobile'])){
            $qq=preg_match_all("/^1[3456789]\d{9}$/", $data['mobile'], $mobiles);
            if($qq==0){
                return false;
            }
        }
        $mail = $data['email'];  //邮箱地址
        $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/";
        preg_match($pattern, $mail, $matches);
        if(empty($matches))$this->error(__('邮箱错误'));

        $dele=BydUserSale::where('user_id',$data['user_id'])->where('state',2)->delete();
        $find=BydUserSale::where('user_id',$data['user_id'])->where('state','<',2)->where('level',$data['level'])->find();
        if(empty($find)){
            $add=BydUserSale::create($data);
        }else{
            $this->error(__('您已提交审核,请耐心等待'));
        }

        if($add){
            \think\Cache::set('bbs',1,3600);
            $this->success(__('资料提交成功，请耐心等待审核'));
        }else{
            $this->error(__('服务器繁忙,请稍后重试'));
        }
    }
    /**
     * 业务地图提交申请
     * @param integer $cate` '0为签约楼盘1为签约区域',
     * @param integer $bid` '楼盘id',
     * @param integer $user_id` '用户id',
     * @param integer $sole_type` '身份0为b1为d',
     * cate 0为签约楼盘1为签约区域
     * bid 楼盘id
     * user_id  用户id
     * sole_type 身份0为b1为d
     */
    public function tjsq(){
        $data = $this->request->request('');
        $user=\app\admin\model\BydUser::find($data['user_id']);
        if(empty($user))  $this->error(__('用户不存在'));

        if($data['cate']==0){
//            $finds=SoleAgency::where('user_id',$data['user_id'])->where('state','<',2)->where('sole_type',2)->find();
//            if(!empty($finds))   $this->error(__('您已提交审核,请等待平台审核'));
            $find=SoleAgency::where('user_id',$data['user_id'])->where('bid',$data['bid'])->where('sole_type',$data['sole_type'])->where('state','<',2)->find();
            if(!empty($find))$this->error(__('您已申请,请勿重复申请'));
            $find=SoleAgency::where('state',0)->where('bid',$data['bid'])->where('sole_type',$data['sole_type'])->find();
            if(!empty($find))$this->error(__('此楼盘已被他人申请,请等待审核结果'));
            $find=SoleAgency::where('state',1)->where('bid',$data['bid'])->where('sole_type',$data['sole_type'])->find();
            if(!empty($find))$this->error(__('此楼盘已被申请'));
            $bulid=SoleBuilding::find($data['bid']);
            $find1=BydDesignerArea::find($bulid['area_id']);//区
            $find2=BydDesignerArea::find($find1['pid']);//市
           $find3=BydDesignerArea::find($find2['pid']);//省
            $ss=$find1['title'];
            $sss=$find2['title'];
            $sh=$find3['title'];
            $cre['address']="$sh/$sss/$ss";
            $cre['bname']=$bulid['title'];
            $cre['username']=$user['username'];
            $cre['bid']=$data['bid'];
            $cre['user_id']=$data['user_id'];
            $cre['sole_type']=$data['sole_type'];
            $creat=SoleAgency::create($cre);
            if($creat){
                \think\Cache::set('sqsq',1,3600);
                $this->success(__('楼盘申请成功，请留意系统消息查看审核结果。'));
            }else{
                $this->error(__('服务器繁忙,请稍后重试'));
            }
        }else{
//            $finds=SoleAgency::where('user_id',$data['user_id'])->where('state','<',2)->where('sole_type','<',2)->find();
//            if(!empty($finds))   $this->error(__('您已提交审核,请等待平台审核'));
            $findqqq=BydDesignerArea::where('id',$data['bid'])->find();
            $find=SoleAgency::where('user_id',$data['user_id'])->where('bid',$data['bid'])->where('state','<',2)->find();
            if(!empty($find))$this->error(__('您已申请,请勿重复申请'));
            $find=SoleAgency::where('state',0)->where('bid',$data['bid'])->find();
            if(!empty($find))$this->error(__('此区域已被他人申请,请等待审核结果'));
            $find=SoleAgency::where('state',1)->where('bid',$data['bid'])->find();
            if(!empty($find))$this->error(__('此区域已被申请'));
            $find1=BydDesignerArea::find($data['bid']);//区
            $find2=BydDesignerArea::find($find1['pid']);//市
            $find3=BydDesignerArea::find($find2['pid']);//省
            $ss=$find1['title'];
            $sss=$find2['title'];
            $sh=$find3['title'];
            $cre['address']="$sh/$sss/$ss";
            $cre['bname']=$findqqq['title'];
            $cre['username']=$user['username'];
            $cre['bid']=$data['bid'];
            $cre['user_id']=$data['user_id'];
            $cre['sole_type']=2;
            $creat=SoleAgency::create($cre);
            if($creat){
                \think\Cache::set('sqsq',1,3600);
                $this->success(__('区域申请成功，请留意系统消息查看审核结果。'));
            }else{
                $this->error(__('服务器繁忙,请稍后重试'));
            }
        }


    }
    /**
     * 测试
     */
    public function qqq(){
        $name='楼盘1111';
        $bz='毛胚';
        $adedress='成都市';
        $time=time();
        $huxin='大户型';
        $image=[
            'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2F09%2F3a%2Fbc%2F093abce7b31f4c8ffdbf345375ff4abb.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649666490&t=80e0fdd6612e35c771dc378e3d043d28',
				"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F42679d1a2a49e8419cfa88a5d029b8fa4091db5249b7a-JN8fIB_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649666516&t=0dcf1f472395107e9f9f10dceafb4042" ,
				"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fmobile%2F2%2F59ccc47e4623f.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649666516&t=55bd62b24de60dc90bab30edd352b600"

        ];
        $path= APP_PATH.'api/view/demo/abc.html';
        $opempath=fopen($path,'a+');
        $arr=fread($opempath,filesize($path));
        $arr=str_replace('{$name}',$name,$arr);
        $arr=str_replace('{$bz}',$bz,$arr);
        $arr=str_replace('{$address}',$adedress,$arr);
        $arr=str_replace('{$time}',$time,$arr);
        $arr=str_replace('{$huxin}',$huxin,$arr);
        $imagestring='';
        foreach ($image as $v){
            $imagestring.='<img src='.$v.'>';
        }
        $arr=str_replace('{$image}',$imagestring,$arr);
        print_r($arr);
    }
    public function likelp(){
        $data = $this->request->request('');
//       print_r($data);die;
//       foreach ($data as $k=>$v){
//           if(!empty($v)){
//               $qq[$k]=['in',$v];
//           }
//       }
        if(!empty($data['sf'])){
            $where['sf']=['in',$data['sf']];
        }
        if(!empty($data['city'])){
            $where['city']=['in',$data['city']];
        }
        if(!empty($data['county'])){
            $where['county']=['in',$data['county']];
        }
        if(!empty($data['loupan'])){
            $where['loupan']=['in',$data['loupan']];
        }
        if(!empty($data['qs'])){
            $where['qs']=['in',$data['qs']];
        }
        if(!empty($data['dh'])){
            $where['dh']=['in',$data['dh']];
        }
        if(!empty($data['lc'])){
            $where['lc']=['in',$data['lc']];
        }
        if(!empty($data['huxin'])){
            $where['huxin']=['in',$data['huxin']];
        }
        if(empty($where)){
            $this->error('请输入筛选条件');
        }
        //print_r($where);die;
//       $qq['choice']=$data['type'];
        $arr=BydDesignerPro::where($where)->select();
        if($arr){
            $this->success(__('请求成功'), ['status' => $arr]);
        } else {
            $this->error('暂无数据');
        }
    }
}
